{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "e34a7645",
   "metadata": {},
   "source": [
    "--------------------\n",
    "* 2002/09/20/week05\n",
    "* python课堂笔记\n",
    "--------------------"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ff2f6fef",
   "metadata": {},
   "source": [
    "# 本周内容\n",
    "\n",
    "### 1.有序的数据结构值列表      \n",
    "### 2.用户输入      \n",
    "### 3.关于格式化文本信息"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "dea80982",
   "metadata": {},
   "outputs": [],
   "source": [
    "网三_01_name = \"Marry\"\n",
    "\n",
    "# 思考：如果想一个变量存放一个班同学的名字，该怎么存？\n",
    "#思考1：处理数据会不会麻烦？"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "1e38c595",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Marry,Alex,Jack,周晓明\n"
     ]
    }
   ],
   "source": [
    "网三_name_list = \"Marry,Alex,Jack,周晓明\"\n",
    "print(网三_name_list)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "9045d471",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Alex'"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 思考2：数据处理会不会麻烦？\n",
    "网三_name_list[6:10]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "29c19ffc",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "19"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 思考3：我想知道这个名单有多少人名？\n",
    "len(网三_name_list)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6dd34cd1",
   "metadata": {},
   "source": [
    "## 1.引入列表的概念\n",
    "* List might contain  items of different types, but usually the items all have the same type\n",
    "> 注释：我们已经学习了数值和字符，list可以方数值和字符，但是通常要么一起放数值，要么一起放字符\n",
    "\n",
    "### 总结一些通用的有序数据结构的方法：\n",
    "> 1.取值：通过索引值取出对应的内容      \n",
    "> 2.切片：通过索引值区间取出对应的一些内容        \n",
    "> 3.in 可以查看成员对象是否存在"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "3bcccbbb",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 3, 4] <class 'list'>\n"
     ]
    }
   ],
   "source": [
    "a = [1,2,3,4]\n",
    "print(a,type(a))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "cc560501",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['Marry', 'Alex', 'Jack', '周晓明'] <class 'list'>\n"
     ]
    }
   ],
   "source": [
    "网三_name_list = [\"Marry\",\"Alex\",\"Jack\",\"周晓明\"]\n",
    "print(网三_name_list,type(网三_name_list))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "afd19bb5",
   "metadata": {},
   "source": [
    "### 1.1 列表的运算法则（Operation)\n",
    "* 列表的数据本身不会改变"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "9d086be8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Marry'"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 取出 网三_name_list 中第一位同学的名字\n",
    "网三_name_list[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "0b2e80bd",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 查看 网三_name_list 有多少位同学\n",
    "len(网三_name_list)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "58c2ff50",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "网三： Marry\n",
      "网三： Alex\n",
      "网三： Jack\n",
      "网三： 周晓明\n"
     ]
    }
   ],
   "source": [
    "for i in 网三_name_list:\n",
    "    print(\"网三：\",i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "244a99c9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# in 可以查询数据是否存在\n",
    "\"Jack\" in 网三_name_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "aabc07d8",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 关于运算法则( in + 切片 取值 len(s) s.count)\n",
    "网三_name_list_转专业 = [\"Xiaoming\",\"Lihua\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "a1b8047e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Marry', 'Alex', 'Jack', '周晓明', 'Xiaoming', 'Lihua']"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网三_name_list + 网三_name_list_转专业"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "6b313281",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 思考：一个list 可不可以出现 重复项？\n",
    "网一_age_list = [18,19,19,19,20,19]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "86cfd91d",
   "metadata": {},
   "source": [
    "### 2.2 列表的方法（Function)---- 对列表数据\n",
    "> 1.增      \n",
    "> 2.删       \n",
    "> 3.改     \n",
    "> 4.查\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "13f3907a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Marry', 'Alex', 'Jack', '周晓明']"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网三_name_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "88c4c223",
   "metadata": {},
   "outputs": [],
   "source": [
    "网三_name_list.append(\"田柾国\")\n",
    "#运行了三次 则结果出现三个“田柾国"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "014457e5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Marry', 'Alex', 'Jack', '周晓明', '田柾国', '田柾国', '田柾国']"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网三_name_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "d8965a76",
   "metadata": {},
   "outputs": [],
   "source": [
    "网三_name_list.append(\"vvv\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "2e6274db",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Marry', '李华', 'Alex', 'Jack', '周晓明', '田柾国', '田柾国', '田柾国', 'vvv']"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网三_name_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "124a2c93",
   "metadata": {},
   "outputs": [],
   "source": [
    "# insert 增加元素到某个位置"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "f75b1e99",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function insert:\n",
      "\n",
      "insert(index, object, /) method of builtins.list instance\n",
      "    Insert object before index.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(网三_name_list.insert)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "bf834350",
   "metadata": {},
   "outputs": [],
   "source": [
    "网三_name_list.insert(1,\"李华\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "ba8d5076",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Marry', '李华', 'Alex', 'Jack', '周晓明', '田柾国', '田柾国', '田柾国', 'vvv']"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网三_name_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "9e29140c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function remove:\n",
      "\n",
      "remove(value, /) method of builtins.list instance\n",
      "    Remove first occurrence of value.\n",
      "    \n",
      "    Raises ValueError if the value is not present.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(网三_name_list.remove)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "8c3037a7",
   "metadata": {},
   "outputs": [],
   "source": [
    "网三_name_list.remove(\"Alex\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "521310c0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Marry', '李华', 'Jack', '周晓明', '田柾国', '田柾国', '田柾国', 'vvv']"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网三_name_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "id": "c772f5be",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Marry', '李华', 'Jack', '周晓明', '田柾国', '田柾国', '田柾国', 'vvv']"
      ]
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# remove 经常和   in    一起使用，先检查成员变量是否存在，然后再remove\n",
    "if \"Alex\" in 网三_name_list:\n",
    "    网三_name_list.remove(\"Alex\")\n",
    "网三_name_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "id": "9e9d468c",
   "metadata": {},
   "outputs": [],
   "source": [
    "# pop "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "id": "916dda8f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function pop:\n",
      "\n",
      "pop(index=-1, /) method of builtins.list instance\n",
      "    Remove and return item at index (default last).\n",
      "    \n",
      "    Raises IndexError if list is empty or index is out of range.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(网三_name_list.pop)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "id": "e12fc932",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'田柾国'"
      ]
     },
     "execution_count": 51,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网三_name_list.pop()\n",
    "# 运行了两次，第一次为vvv"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "id": "d5682474",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'周晓明'"
      ]
     },
     "execution_count": 53,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网三_name_list.pop(2)\n",
    "# 运行了两次，第一次为Jack"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "id": "ba2757e9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Marry', '李华', '田柾国', '田柾国']"
      ]
     },
     "execution_count": 54,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网三_name_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "id": "ece368a1",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 实践：请移除19的元素\n",
    "age_list = [18,19,18,19,17,20,20,18,19,16,17,20]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "id": "bff131cd",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3"
      ]
     },
     "execution_count": 58,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "age_list.count(19)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "id": "d239e8b6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[18, 19, 18, 19, 17, 20, 20, 18, 19, 16, 17, 20]"
      ]
     },
     "execution_count": 59,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "age_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "id": "00ee69b7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[18, 18, 17, 20, 20, 18, 16, 17, 20]"
      ]
     },
     "execution_count": 61,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 思路1：remove(value),先决条件，知道值出现的次数\n",
    "\n",
    "删除的数量 = age_list.count(19)\n",
    "for i in range(删除的数量):\n",
    "    if 19 in age_list:\n",
    "        age_list.remove(19)\n",
    "    age_list\n",
    "age_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 76,
   "id": "56a0b5a8",
   "metadata": {},
   "outputs": [],
   "source": [
    "age_list = [18,19,18,19,17,20,20,18,19,16,17,20]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 77,
   "id": "fd46d576",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[18, 19, 18, 19, 17, 20, 20, 18, 19, 16, 17, 20]"
      ]
     },
     "execution_count": 77,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "age_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 78,
   "id": "db09ab05",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1 19\n",
      "2 19\n",
      "6 19\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "[18, 18, 17, 20, 20, 18, 16, 17, 20]"
      ]
     },
     "execution_count": 78,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 思路2： pop(index),先决条件，知道值出现的位置\n",
    "for i,v in enumerate(age_list):\n",
    "    if v == 19:\n",
    "        print(i,v)\n",
    "        age_list.pop(i)\n",
    "age_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "0aaed3a1",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 课后练习： \n",
    "# 1,课本P68 AND P81\n",
    "# 2.剩下的 reserve,extend,clear,copy,sort  尝试练习\n",
    "# 3.总结列表的方法，用表格的形式写在markdown上"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "452a51ad",
   "metadata": {},
   "source": [
    "### 课后作业"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "3f8ca00c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Don't panic!\n",
      "['D', 'o', 'n', \"'\", 't', ' ', 'p', 'a', 'n', 'i', 'c', '!']\n"
     ]
    }
   ],
   "source": [
    "# P68\n",
    "phrase = \"Don't panic!\"\n",
    "plist = list(phrase)\n",
    "print(phrase)\n",
    "print"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "c454c24d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Don't panic!\n",
      "['D', 'o', 'n', \"'\", 't', ' ', 'p', 'a', 'n', 'i', 'c', '!']\n",
      "['o', 'n', ' ', 't', 'a', 'p']\n",
      "on tap\n"
     ]
    }
   ],
   "source": [
    "phrase = \"Don't panic!\"\n",
    "plist = list(phrase)\n",
    "print(phrase)\n",
    "print(plist)\n",
    "\n",
    "for i in range(4):\n",
    "    plist.pop()\n",
    "plist.pop(0)\n",
    "plist.remove(\"'\")\n",
    "plist.extend([plist.pop(),plist.pop()])\n",
    "plist.insert(2,plist.pop(3))\n",
    "\n",
    "new_phrase = ''.join(plist)\n",
    "print(plist)\n",
    "print(new_phrase)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "65bc4743",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Don't panic!\n",
      "['D', 'o', 'n', \"'\", 't', ' ', 'p', 'a', 'n', 'i', 'c', '!']\n"
     ]
    }
   ],
   "source": [
    "# P82\n",
    "phrase = \"Don't panic!\"\n",
    "plist = list(phrase)\n",
    "print(phrase)\n",
    "print(plist)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "d06e3473",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['D', 'o', 'n', \"'\", 't', ' ', 'p', 'a', 'n', 'i', 'c', '!']\n",
      ".join(plist[1:3]).join({plist[5],plist[4],plist[7],plist[6]})\n"
     ]
    }
   ],
   "source": [
    "new_phrase = \".join(plist[1:3])\"\n",
    "new_phrase = new_phrase +\".join({plist[5],plist[4],plist[7],plist[6]})\"\n",
    "print(plist)\n",
    "print(new_phrase)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "886cea59",
   "metadata": {},
   "source": [
    "## 总结"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ae7a02e5",
   "metadata": {},
   "source": [
    "||append|count|extend|index|insert|           \n",
    "| :-----: | :----: | :----: | :----: | :----: | :----: |\n",
    "|功能|在列表的末尾添加新的对象|用于统计某个元素在列表中出现的次数用于在列表的末尾一次性添加另一个序列中的多个值（序列扩展）|用于从列表中找出某一个值第一个匹配项的索引位置|用于将某一对象插入列表中|          \n",
    "|格式|list.sppend（obj）|list.count（obj)|list.extend（obj）|list.index（obj）|list.insert（index，obj）|"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9acb1d28",
   "metadata": {},
   "source": [
    " ||pop |  remove |  reverse | sort |  clear | copy |\n",
    " | :-----: | :----: | :----: | :----: | :----: | :----: | :----: |\n",
    " | 功能 | 用于移除列表中的一个元素（默认为最后一个，但可指定位置），并返回该元素的值 | 用于移除列表中某一值的第一个匹配项 | 用于将列表中元素进行反向操作|用于对原始序列进行排序操作，默认为升序；当然还可以指定参数，实现指定方法的排序操作|用于清空列表，类似于 del list[ : ]。|用于复制列表，类似于 list[ : ]。\n",
    " | 格式 | list.pop（obj） | list.remove（obj） | list.reverse() | list.sort（func）| list.clear（） | list.copy（） |"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
